//
//  ZUIImageView.m
//  danpin
//
//  Created by chuangjia on 17/6/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ZUIImageView.h"
#import "UIImageView+WebCache.h"
#import "viewconfig.h"
@interface ZUIImageView()
{
    
}
@property(nonatomic,strong) UIView *bgView;

@end

@implementation ZUIImageView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _bgView =[[UIView alloc] initWithFrame:frame];
        _bgView.backgroundColor =[UIColor whiteColor];
        [self addSubview:_bgView];
        //
        _iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_iconView];
        self.placeholderImage =[UIImage imageNamed:@"image.png"];
    }
    return self;
}
-(void)dealloc
{
    self.bgView=nil;
    self.iconView=nil;
    self.imageUrl =nil;
    self.placeholderImage =nil;
//        NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.bgView.frame =self.bounds;
    
}
-(void)setImageUrl:(NSString *)imageUrl
{
    if (imageUrl!=nil&&imageUrl.length>0) {
        __block UIActivityIndicatorView *activityIndicator=nil;
        __weak UIImageView *weakImageView = self.iconView;
        self.iconView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        [weakImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.backgroundColor=[UIColor clearColor];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                image=self.placeholderImage;
                weakImageView.image=self.placeholderImage;
            }
            CGFloat w =image.size.width;
            CGFloat h =image.size.height;
            
            if(w > h)
            {
            
                CGFloat r = h / w;
                
                
                
                if(w > self.bounds.size.width)
                    w = self.bounds.size.width;
                
                h = w * r;

                
            }
            else
            {
                CGFloat r = w / h;
                
                if(h > self.bounds.size.height)
                    h = self.bounds.size.height;
                
                w = h * r;
            }
            self.iconView.frame = CGRectMake((self.bounds.size.width-w)/2, (self.bounds.size.height -h)/2, w, h);
        
            [weakImageView.layer addAnimation:[CATransition animation] forKey:kCATransition];

           [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        
    }
}
@end
